package com.rybbaby.tss.core.listener;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.io.IOUtils;

/**
 * @author 谢进伟
 * @description 服务器启动停止banner监听
 * @createDate 2017/3/3 11:31
 */
public class BannerListener implements ServletContextListener {
	
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		showBanner(sce , "startBanner.txt");
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		showBanner(sce , "stopBanner.txt");
	}
	
	private void showBanner(ServletContextEvent sce , String path) {
		ClassLoader classLoader = this.getClass().getClassLoader();
		try (InputStream bannerStream = classLoader.getResourceAsStream(path)) {
			if(bannerStream != null) {
				String banner = IOUtils.toString(bannerStream, StandardCharsets.UTF_8);
				System.out.println(banner);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
